package com.huawei.hwclouds.lubanops.demo.order.biz.conf;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSourceFactory;

@Configuration
@MapperScan(basePackages = "com.huawei.hwclouds.lubanops.demo.order.biz.mapper",
    sqlSessionFactoryRef = "orderBizSqlSessionFactory")
public class OrderDataSourceConfig {

    private static final Logger logger = LoggerFactory.getLogger(OrderDataSourceConfig.class);

    @Value("${spring.datasource.order.url}")
    private String dbUrl;

    @Value("${spring.datasource.order.username}")
    private String dbUserName;

    @Value("${spring.datasource.order.password}")
    private String dbPassword;

    @Value("${spring.datasource.order.driver-class-name}")
    private String dbDriverClassName;

    @Value("${spring.datasource.order.max-active}")
    private String maxActive;

    /**
     * 创建数据源
     */
    @Bean(name = "orderBizDatasource")
    public DataSource getDataSource() throws Exception {
        // 调用资源文件中的变量
        Properties p = new Properties();

        p.put("driverClassName", dbDriverClassName);
        p.put("url", dbUrl);
        p.put("username", dbUserName);
        p.put("maxActive", maxActive);
        // 密码解密
        String password = dbPassword;
        p.put("password", password);
        p.put("validationQuery", "select 1");
        p.put("testWhileIdle", "true");
        return DruidDataSourceFactory.createDataSource(p);

    }

    /**
     * 根据数据源创建SqlSessionFactory
     */
    @Bean(name = "orderBizSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("orderBizDatasource") DataSource dataSource)
        throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 指定数据源
        sqlSessionFactoryBean.setDataSource(dataSource);

        // 指定xml文件位置
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(
            "classpath:com/huawei/hwclouds/lubanops/demo/order/biz/mapper/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }

    @Bean(name = "orderBizTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("orderBizDatasource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "orderBizSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(
        @Qualifier("orderBizSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
